package com.icim.hcbh.export.service;

import java.math.BigDecimal;
import java.net.URL;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.icim.cxbh.export.pojo.BoxInfo;
import com.icim.cxbh.export.pojo.EaInfo;
import com.icim.cxbh.export.pojo.OqcInfo;
import com.icim.exception.ICIMException;
import com.icim.hcbh.export.base.ClientInfo;
import com.icim.hcbh.export.base.ComWsService;
import com.icim.hcbh.export.base.HcbhIqcEaInfo;
import com.icim.hcbh.export.base.HcbhIqcEaInfoId;
import com.icim.hcbh.export.base.HcbhOqcEaInfo;
import com.icim.hcbh.export.base.HcbhOqcEaInfoId;
import com.icim.hcbh.export.base.HcbhOqcInfo;
import com.icim.hcbh.export.base.HcbhOqcInfoId;
import com.icim.hcbh.export.base.IComWs;

@Service("hcbhDataImportService")
public class HcbhDataImportServiceImpl implements HcbhDataImportService {

	private static final Logger LOGGER = Logger.getLogger(HcbhDataImportServiceImpl.class);

	private static final QName SERVICE_NAME = new QName("http://common.ws.bh.avic/", "ComWsService");

	@Override
	public void importData(OqcInfo inObj) {

		URL wsdlURL = ComWsService.WSDL_LOCATION;
		ComWsService ss = new ComWsService(wsdlURL, SERVICE_NAME);
		IComWs port = ss.getComWsPort();

		ClientInfo ci = new ClientInfo();
		// 用户名
		ci.setUserName("hcbh");
		// 密码
		ci.setUserPwd("hcbh111");

		List<BoxInfo> boxInfoList = inObj.getBoxInfoList();

		XMLGregorianCalendar currentUpdDate = convertToXMLGregorianCalendar(new Date());

		for (BoxInfo boxInfo : boxInfoList) {

			LOGGER.info("Import box " + boxInfo.getBoxId());

			// set oqcinfo process
			HcbhOqcInfo hcbhOqcInfo = new HcbhOqcInfo();

			HcbhOqcInfoId hcbhOqcInfoId = new HcbhOqcInfoId();
			hcbhOqcInfoId.setBoxId(boxInfo.getBoxId());
			hcbhOqcInfo.setId(hcbhOqcInfoId);

			hcbhOqcInfo.setModel(boxInfo.getModel());
			hcbhOqcInfo.setStkinDate(convertToXMLGregorianCalendar(boxInfo.getStkinDate()));
			hcbhOqcInfo.setUpdDate(currentUpdDate);
			hcbhOqcInfo.setWorkQty(BigDecimal.valueOf(boxInfo.getWorkQty()));

			List<EaInfo> eaInfos = boxInfo.getEaInfos();

			for (EaInfo eaInfo : eaInfos) {

				HcbhOqcEaInfo oqceainfo = new HcbhOqcEaInfo();
				HcbhOqcEaInfoId oqceainfoid = new HcbhOqcEaInfoId();

				oqceainfoid.setBoxId(boxInfo.getBoxId());
				oqceainfo.setId(oqceainfoid);

				oqceainfo.setEaId(eaInfo.getEaId());
				oqceainfo.setUpdDate(currentUpdDate);
				com.icim.hcbh.export.base.Rmsg retMesg = port.createOrUpdateOQCEAINFO(ci, oqceainfo);
				if (!retMesg.isRb()) {
					throw new ICIMException(retMesg.getRmsg());
				}

				// set Iqceainfo process
				HcbhIqcEaInfo iqceainfo = new HcbhIqcEaInfo();
				HcbhIqcEaInfoId iqceainfoid = new HcbhIqcEaInfoId();

				iqceainfoid.setEaId(eaInfo.getEaId());
				iqceainfo.setId(iqceainfoid);

				iqceainfo.setUpdDate(currentUpdDate);
				iqceainfo.setInputDate(convertToXMLGregorianCalendar(boxInfo.getStkinDate()));
				iqceainfo.setModel(boxInfo.getModel());
				retMesg = port.createOrUpdateIQCEAINFO(ci, iqceainfo);
				if (!retMesg.isRb()) {
					throw new ICIMException(retMesg.getRmsg());
				}
			}

			com.icim.hcbh.export.base.Rmsg retMesg = port.createOrUpdateOQCINFO(ci, hcbhOqcInfo);
			if (!retMesg.isRb()) {
				throw new ICIMException(retMesg.getRmsg());
			}
		}

	}

	public static XMLGregorianCalendar convertToXMLGregorianCalendar(Date date) {

		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		XMLGregorianCalendar gc = null;
		try {
			gc = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
		} catch (Exception e) {

			e.printStackTrace();
		}
		return gc;
	}

}
